package com.zou.jokes.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zou.jokes.Constant;
import com.zou.jokes.R;
import com.zou.jokes.utils.DisplayUtil;
import com.zou.jokes.utils.RxBus;
import com.zou.jokes.utils.SharedPreferencesUtils;
import com.zou.jokes.utils.UserEvent;

/**
 * Created by zou on 2016/6/4.
 */
public class SettingFragment extends Fragment {

    private TextView content;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View        view         = inflater.inflate(R.layout.fragment_setting, container, false);
        RadioGroup  radioGroup;
        RadioButton btn_xiao;
        RadioButton btn_zhong;
        RadioButton btn_da;
        View        include_joke = view.findViewById(R.id.include_joke);
        radioGroup = (RadioGroup) view.findViewById(R.id.rg);
        btn_xiao = (RadioButton) view.findViewById(R.id.btn_xiao);
        btn_zhong = (RadioButton) view.findViewById(R.id.btn_zhong);
        btn_da = (RadioButton) view.findViewById(R.id.btn_da);
        content = (TextView) include_joke.findViewById(R.id.joke_content);

        resize();

        String size = (String) SharedPreferencesUtils.getParam(getContext(), Constant.TEXTSIZE, "20");
        Log.e("wocao", "onCreateView: " + size);
        assert size != null;
        switch (size) {
            case "20":
                btn_xiao.setChecked(true);
                break;
            case "24":
                btn_zhong.setChecked(true);
                break;
            case "28":
                btn_da.setChecked(true);
                break;
            default:
                break;
        }


        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.btn_xiao:
                    SharedPreferencesUtils.setParam(getContext(), Constant.TEXTSIZE, "20");
                    break;
                case R.id.btn_zhong:
                    SharedPreferencesUtils.setParam(getContext(), Constant.TEXTSIZE, "24");
                    break;
                case R.id.btn_da:
                    SharedPreferencesUtils.setParam(getContext(), Constant.TEXTSIZE, "28");
                    break;
            }

            resize();
            RxBus.getDefault().post(new UserEvent(Constant.UPDATEJOKE, ""));
        });
        return view;
    }

    private void resize() {
        String textSize = (String) SharedPreferencesUtils.getParam(getContext(), Constant.TEXTSIZE, "20");
//        DisplayUtil.sp2px(getContext(), Float.valueOf(textSize))
        content.setTextSize(Float.parseFloat(textSize));
    }
}
